package cn.virens.web.components.shiro;

import java.util.Map;

import javax.servlet.Filter;

import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.util.CollectionUtils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

public class CustomShiroFilterFactoryBean extends ShiroFilterFactoryBean implements ApplicationContextAware {

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		Map<String, Filter> filters = getFilters();
		// 判断过滤器是否实现ApplicationContextAware 接口，如果实现了就调用
		if (!CollectionUtils.isEmpty(filters)) {
			for (Filter filter : filters.values()) {
				if (filter instanceof ApplicationContextAware) {
					((ApplicationContextAware) filter).setApplicationContext(applicationContext);
				}
			}
		}
	}

	public void addFilter(String definition, Filter filter) {
		this.getFilters().put(definition, filter);
	}

	public void addFilterChain(String antPath, String definition) {
		this.getFilterChainDefinitionMap().put(antPath, definition);
	}
}
